package snailAI.EmailSending;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
import snailAI.Utils.EmailUtils;

import java.io.IOException;

@Component
public class EmailConsumer {

    public static final String bossEmail_1="3181725457@qq.com";
    public static final String bossEmail_2="17721858959@88.com";

    @Autowired
    public EmailUtils emailUtils;

    // 监听队列1
    @RabbitListener(queues = "emailQue1")
    public void sendToEmail_1(String msg, Channel channel, Message message) throws IOException {
        channel.basicQos(1);
        try {
            System.out.println("发送了！！！！！！！");
            emailUtils.sendNormalMsg(msg, bossEmail_1);
            // 手动确认消息：告诉 RabbitMQ 消息已处理，从队列删除
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 手动拒绝消息：告诉 RabbitMQ 消息处理失败，直接丢弃（不放回队列）
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

    // 监听队列2：同样添加手动确认
    @RabbitListener(queues = "emailQue2")
    public void sendToEmail_2(String msg, Channel channel, Message message) throws IOException, IOException {
        channel.basicQos(1);
        try {
            emailUtils.sendNormalMsg(msg, bossEmail_2);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
